package com.hww.acommon.test;

import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.hww.acommon.R;
import com.hww.acommon.utils.http.OkHttpUtils;
import com.hww.acommon.utils.http.callback.OnDownloadListener;
import com.hww.common.MyAppThis;
import com.hww.common.util.LogUtils;
import com.hww.common.util.ThreadUtils;
import com.hww.common.util.ToastUtils;
import com.hww.common.view.MiniLoadingView;
import com.hww.common.view.button.RippleView;
import com.hww.common.view.rolling.ComplexItemEntity;
import com.hww.common.view.rolling.ComplexViewMF;
import com.hww.common.view.rolling.DisplayEntity;
import com.hww.common.view.rolling.MarqueeFactory;
import com.hww.common.view.rolling.MarqueeTextView;
import com.hww.common.view.rolling.MarqueeView;
import com.hww.common.view.rolling.SimpleNoticeMF;

import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author hww
 * @date 0017 2024/6/17
 * email: heww@zhongyuntech.cn
 * description:
 */
public class ViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);

        RippleView one = (RippleView) findViewById(R.id.ripple_one);
        RippleView two = (RippleView) findViewById(R.id.ripple_two);
        RippleView three = (RippleView) findViewById(R.id.ripple_three);

        one.setRippleType(RippleView.RippleType.SIMPLE);
        two.setRippleType(RippleView.RippleType.DOUBLE);
        three.setRippleType(RippleView.RippleType.RECTANGLE);


        MarqueeTextView mTvMarquee = (MarqueeTextView) findViewById(R.id.marquee_text_view);
        mTvMarquee.setTextSize(45);
        final List<String> datas = Arrays.asList("《赋得古原草送别》", "离离原上草，一岁一枯荣。", "野火烧不尽，春风吹又生。", "远芳侵古道，晴翠接荒城。", "又送王孙去，萋萋满别情。");

        mTvMarquee.setOnMarqueeListener(new MarqueeTextView.OnMarqueeListener() {
            @Override
            public DisplayEntity onStartMarquee(DisplayEntity displayMsg, int index) {
                if ("离离原上草，一岁一枯荣。".equals(displayMsg.toString())) {
                    return null;
                } else {
                    ToastUtils.success(ViewActivity.this, "开始滚动：" + displayMsg).show();
                    return displayMsg;
                }
            }

            @Override
            public List<DisplayEntity> onMarqueeFinished(List<DisplayEntity> displayDatas) {
                ToastUtils.success(ViewActivity.this, "一轮滚动完毕！").show();
                return displayDatas;
            }
        });
        mTvMarquee.startSimpleRoll(datas);

        MarqueeView marqueeView1 = (MarqueeView) findViewById(R.id.marqueeView);
        MarqueeFactory<TextView, String> marqueeFactory1 = new SimpleNoticeMF(MyAppThis.getThis());
        marqueeView1.setMarqueeFactory(marqueeFactory1);
        marqueeView1.startFlipping();
        marqueeFactory1.setOnItemClickListener((view, holder) -> ToastUtils.success(ViewActivity.this, holder.getData()).show());
        marqueeFactory1.setData(datas);

        marqueeView1.setMarqueeFactory(marqueeFactory1);
        marqueeView1.setAnimDuration(15000);
        marqueeView1.setInterval(16000);
        marqueeView1.startFlipping();


        MarqueeView marqueeView2 = (MarqueeView) findViewById(R.id.marqueeView1);
        List<ComplexItemEntity> complexDatas = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            complexDatas.add(new ComplexItemEntity("标题 " + i, "副标题 " + i, "时间 " + i));
        }
        MarqueeFactory<RelativeLayout, ComplexItemEntity> marqueeFactory5 = new ComplexViewMF(MyAppThis.getThis());
        marqueeFactory5.setOnItemClickListener((view, holder) -> ToastUtils.success(MyAppThis.getThis(), holder.getData().toString()).show());
        marqueeFactory5.setData(complexDatas);
        marqueeView2.setAnimInAndOut(R.anim.marquee_top_in, R.anim.marquee_bottom_out);
        marqueeView2.setMarqueeFactory(marqueeFactory5);
        marqueeView2.startFlipping();

        addMiniLoadingView();

        System.out.printf("");
    }

    private void addMiniLoadingView(){
        MiniLoadingView miniLoadingView = new MiniLoadingView(ViewActivity.this);
        miniLoadingView.setSize(120);
        FrameLayout frameLayout= (FrameLayout)this.findViewById(R.id.frame_layout_add_mini_loading_view);
        frameLayout.addView(miniLoadingView);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.gravity = Gravity.CENTER;
        frameLayout.setLayoutParams(layoutParams);
    }

    private void mToast(String msg) {
        ToastUtils.normal(MyAppThis.getThis(), msg).show();
    }


    public void onClickTestNetWork(View view) {

        ThreadUtils.getSinglePool().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    String url1 = "https://jsonplaceholder.typicode.com/posts/1";/*get*/
                    String s = OkHttpUtils.doGet(url1, null, null);
                    LogUtils.i(String.format("url=%s\n%s", url1, s));

                    String url2 = "https://jsonplaceholder.typicode.com/posts";
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("title", "foo 000");
                    jsonObject.put("body", "bar 000");
                    jsonObject.put("userId", "1 000");
                    String s1 = OkHttpUtils.doPostJson(url2, jsonObject.toString(), null);
                    LogUtils.i(String.format("url=%s\n%s", url2, s1));

                    String url3 = "http://httpbin.org/post";
                    Map<String, String> f = new HashMap<>();
                    f.put("key1", "value1");
                    f.put("key2", "value2");
                    String s2 = OkHttpUtils.doPostForm(url3, f, null);
                    LogUtils.i(String.format("url=%s\n%s", url3, s2));

//                String url = "http://nas.zytech.site:8001/eam/update/update_vnc.txt";
                    String url = "http://nas.zytech.site:8001/zykj/zykj-v4.0.1.11-.apk";

                    String path_b = "/sdcard/ZY/zykj-v4.0.1.11-.apk.1";
                    boolean b = OkHttpUtils.downLoadFile(url, path_b);
                    LogUtils.i(String.format("OkHttpUtils.downLoadFile=%s\n%s", b, path_b));

                    String path = "/sdcard/ZY/zykj-v4.0.1.11-.apk";
                    OkHttpUtils.downloadWithResume(url, new File(path), new OnDownloadListener() {
                        @Override
                        public void onDownloadSuccess(String path, String des) {
                            LogUtils.i(String.format("onDownloadSuccess===> path=%s  des=%s", path, des));
                        }

                        @Override
                        public void onDownloading(int progress) {
                            Log.i("netWork", String.format("onDownloading===> %s", progress));
                        }

                        @Override
                        public void onDownloadFailed(String des) {
                            LogUtils.i(String.format("onDownloadFailed===> %s", des));
                        }
                    });


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private void startNetDiagnosis() {
//        StringBuilder builder = new StringBuilder();
//        String host = "www.baidu.com";
//
//        builder.append("开始诊断.....").append("\n");
//        String packageCode = PackageInfo.getPackageCode(MyAppThis.getThis());
//        builder.append("versionCode:\t").append(packageCode).append("\n");
//        String packageName = PackageInfo.getPackageName(MyAppThis.getThis());
//        builder.append("versionName\t").append(packageName).append("\n");
//        builder.append("机器类型:\t" + android.os.Build.MANUFACTURER + " : " + android.os.Build.BRAND + " : " + android.os.Build.MODEL).append("").append("\n");
//        builder.append("系统版本:\t" + android.os.Build.VERSION.RELEASE).append("\n");
//        builder.append("运营商：\t").append(NetworkUtils.getNetworkOperatorName()).append("\n");
//        try {
//            TelephonyManager telephonyManager = (TelephonyManager) MyAppThis.getThis().getSystemService(Context.TELEPHONY_SERVICE);
//            builder.append("ISOCountryCode:\t").append(telephonyManager.getNetworkCountryIso()).append("\n");
//
//            builder.append("机器ID:\t").append(telephonyManager.getDeviceId()).append("\n");
//
//            String tmp = telephonyManager.getNetworkOperator();
//            if (tmp.length() >= 3) {
//                String mobileCountryCode = tmp.substring(0, 3);
//                builder.append("MobileCountryCode:\t").append(mobileCountryCode).append("\n");
//            }
//            if (tmp.length() >= 5) {
//                String mobileNetCode = tmp.substring(3, 5);
//                builder.append("MobileNetworkCode:\t").append(mobileNetCode).append("\n");
//            }
//        } catch (Throwable e) {
//        }
//
//        builder.append("\n");
//        builder.append("诊断域名：\t").append(host).append("\n");
//
//        boolean connected = NetworkUtils.isConnected();
//        builder.append("当前是否联网: \t").append(connected ? "已联网" : "未联网").append("\n");
//        builder.append("当前联网类型:\t").append(NetworkUtils.getNetworkType()).append("\n");
//        builder.append("本地IP：\t").append(NetworkUtils.getIPAddress(true)).append("\n");
//        builder.append("本地网关：\t").append(NetworkUtils.getGatewayByWifi()).append("\n");
//        builder.append("本地DNS：\t").append(NetworkUtils.getLocalDns("dns1")).append("  ").append(NetworkUtils.getLocalDns("dns2")).append("\n");
//        builder.append("远端域名：\t").append(host).append("\n");
//        List<String> remoteIpList = new ArrayList<>();
//        Map<String, Object> map = NetworkUtils.getDomainIp(host);
//        String useTime = (String) map.get("useTime");
//        InetAddress[] remoteInet = (InetAddress[]) map.get("remoteInet");
//        String timeShow = null;
//        if (Integer.parseInt(useTime) > 5000) {// 如果大于1000ms，则换用s来显示
//            timeShow = " (" + Integer.parseInt(useTime) / 1000 + "s)";
//        } else {
//            timeShow = " (" + useTime + "ms)";
//        }
//        String ipString = "";
//        if (remoteInet != null) {// 解析正确
//            int len = remoteInet.length;
//            for (int i = 0; i < len; i++) {
//                remoteIpList.add(remoteInet[i].getHostAddress());
//                ipString += remoteInet[i].getHostAddress() + ",";
//            }
//            ipString = ipString.substring(0, ipString.length() - 1);
//            builder.append("DNS解析结果:\t" + ipString + timeShow).append("\n");
//        }
//        builder.append("\n");
//        if (connected) {
//            builder.append("开始TCP连接测试...\t").append("").append("\n");
//            builder.append("开始ping...127.0.0.1\t").append("").append("\n");
//            LDNetPing ldNetPing = new LDNetPing(new LDNetPing.LDNetPingListener() {
//                @Override
//                public void OnNetPingFinished(String log) {
//                    builder.append(log).append("\n");
//                }
//            }, 4);
//            ldNetPing.exec("127.0.0.1",false);
//            ldNetPing.exec("127.0.0.1",false);
//            builder.append("\t").append("").append("\n");
//            builder.append("\t").append("").append("\n");
//        } else {
//            builder.append("\n\n当前主机未联网,请检查网络！").append("\n");
//        }
    }
}
